package org.xhnews.bo;

import java.io.InputStream;
import java.util.Vector;

import org.xhnews.constant.Constant;
import org.xhnews.database.DatabaseManager;
import org.xhnews.download.DownloadManager;
import org.xhnews.preferences.Preferences;
import org.xhnews.to.Contact;
import org.xhnews.to.MessageItem;
import org.xhnews.utils.DeviceUtil;
import org.xhnews.utils.StringUtil;

public class ForwardBo {
	/*
	 * 获得所有联系人
	 */
	public static Vector getAllContacts() {
		return DatabaseManager.getAllContacts();
	}
    /*
     * 获得所有消息
     */
	public static Vector getMessages() {
		return DatabaseManager.getMessages();
	}
    /*
     * 根据消息ID获得消息正文
     */
	public static String getMessageContent(String id){
		return DatabaseManager.getMessageContent(id);
	}
	/*
	 * 设置消息为已读状态
	 */
	public static void setMessageItemRead(String msgId) {
		DatabaseManager.setMessageItemRead(msgId, true);
	}
	/*
	 * 更新联系人列表
	 */
	public static void updateContacts() {
		Vector contacts = DownloadManager.downloadContacts(
				Preferences.server_address+Constant.URL_CONTACT, DeviceUtil.getPhoneIMEI());
		int length = contacts.size();
		for (int i = 0; i < length; i++)
			DatabaseManager.insertContacts((Contact) contacts.elementAt(i));
	}
	/*
	 * 更新消息列表
	 */
	public static void updateMessages(){
		 Vector msgs=DownloadManager.downloadMessages(Preferences.server_address+Constant.URL_MESSAGELIST, DeviceUtil.getPhoneIMEI());
		 int length=msgs.size();
		 for(int i=0;i<length;i++){
			 MessageItem item=(MessageItem)msgs.elementAt(i);
			 DatabaseManager.insertMessage(item); 
			 String content=DownloadManager.downloadMessageContent(Preferences.server_address+Constant.URL_MESSAGEVIEW, DeviceUtil.getPhoneIMEI());
			 DatabaseManager.setMessageContent(item.getId(), content);
		 }
	}
	/*
	 * 转发消息
	 */
	public static String forward(String InfoID, Vector receivers,
			String RevTypes[], String Remark) {
		String sendUrl = Preferences.server_address+Constant.URL_FORWARD + "imei="+DeviceUtil.getPhoneIMEI()+"&infoID=" + InfoID + "&RevIDs=";
		for (int i = 0; i < receivers.size(); i++) {
			sendUrl = sendUrl + ((Contact)receivers.elementAt(i)).getId() + ",";
		}
		sendUrl = sendUrl.substring(0, sendUrl.length() - 1);
		sendUrl = sendUrl + "&RevTypes=";
		for (int i = 0; i < RevTypes.length; i++) {
			sendUrl = sendUrl + RevTypes[i] + ",";
		}
		sendUrl = sendUrl.substring(0, sendUrl.length() - 1);
		sendUrl = sendUrl + "&Remark=" + Remark;
		System.out.println(sendUrl);
		InputStream dis = DownloadManager.send(sendUrl);
		return StringUtil.is2String(dis);
	}
}
